package dao;

import domain.EmployeeItem;
import utils.JDBCUtils;
import view.SearchFrame;
import view.SearchResultFrame;

import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;

import static sun.misc.MessageUtils.where;

public class AdminDao {

    //刷新
    public static ArrayList<EmployeeItem> queryAll() {
        ArrayList<EmployeeItem> list = new ArrayList<>();
        try {
            Connection con = JDBCUtils.getConnection();
            Statement stat = con.createStatement();
            String sql = "SELECT * FROM employee";
            ResultSet rs = stat.executeQuery(sql);
            //处理结果
            while (rs.next()) {
                EmployeeItem employeeItem = new EmployeeItem();
                employeeItem.setEid(rs.getString("eid"));
                employeeItem.setEname(rs.getString("ename"));
                employeeItem.setAge(rs.getString("age"));
                employeeItem.setSex(rs.getString("sex"));
                employeeItem.setHirdate(rs.getString("hirdate"));
                list.add(employeeItem);
            }
            JDBCUtils.release(rs, stat, con);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    //添加
    public void addEmployeeItem(EmployeeItem employeeItem) {
        try {
            Connection con = JDBCUtils.getConnection();
            String sql = "INSERT INTO employee VALUES('" + employeeItem.getEid() + "','" + employeeItem.getEname() + "','" + employeeItem.getAge() + "','" + employeeItem.getSex() + "','" + employeeItem.getHirdate() + "')";
            Statement stat = con.createStatement();
            stat.executeUpdate(sql);
            JDBCUtils.release(stat, con);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ;
    }

    //删除
    public boolean deleteEmployeeItem(String eid) {
        try {
            Connection con = JDBCUtils.getConnection();
            String sql = "DELETE FROM employee WHERE eid='" + eid + "'";
            Statement stat = con.createStatement();
            int i = stat.executeUpdate(sql);
            JDBCUtils.release(stat, con);
            if (i == 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //更改
    public void updateEmployeeItem(EmployeeItem employeeItem, String oldEid) {
        try {
            Connection con = JDBCUtils.getConnection();
            String sql = "UPDATE employee SET eid='" + employeeItem.getEid() + "',ename='" + employeeItem.getEname() + "',age='" + employeeItem.getAge() + "',sex='" + employeeItem.getSex() + "',hirdate='" + employeeItem.getHirdate() + "' WHERE eid='" + oldEid + "'";
            Statement stat = con.createStatement();
            int i = stat.executeUpdate(sql);
            JDBCUtils.release(stat, con);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询
    public  void searchEmployeeItem(String eid) {
        try {
            EmployeeItem employeeItem=new EmployeeItem();
            Connection con = JDBCUtils.getConnection();
            Statement stat = con.createStatement();
            String sql = "SELECT * FROM employee WHERE eid='"+eid+"'";
            ResultSet rs = stat.executeQuery(sql);
            //处理结果
            if(rs.next()) {
                employeeItem.setEid(rs.getString("eid"));
                employeeItem.setEname(rs.getString("ename"));
                employeeItem.setAge(rs.getString("age"));
                employeeItem.setSex(rs.getString("sex"));
                employeeItem.setHirdate(rs.getString("hirdate"));
                new SearchResultFrame(employeeItem);
            }
            JDBCUtils.release(rs, stat, con);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //管理员登录
    public boolean adminload(String username,String password){
        try {
            Connection con = JDBCUtils.getConnection();
            String sql = "SELECT * FROM admin WHERE username=? AND passworld = ?";
            PreparedStatement pst = con.prepareStatement(sql);
            //设置占位符
            pst.setObject(1,username);
            pst.setObject(2,password);

            //执行sql语句
            ResultSet rs = pst.executeQuery();

            if (rs.next()){
                return true;
             }
            JDBCUtils.release(rs, pst, con);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //员工注册
    public void register(String username,String password){
        try {
            Connection con = JDBCUtils.getConnection();
            String sql = "INSERT INTO users VALUES('"+username+"','"+password+"')";
            Statement stat = con.createStatement();
            stat.executeUpdate(sql);
            JDBCUtils.release(stat, con);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
